package com.sz.app.customer.api.logistics;

import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.constants.BaseConstants;
import com.sz.biz.logistics.core.dto.*;
import com.sz.biz.logistics.core.entity.ProductServicePropertyEnum;
import com.sz.biz.logistics.base.service.AirportService;
import com.sz.biz.logistics.base.service.PortService;
import com.sz.biz.logistics.core.service.ProductGroupService;
import com.sz.biz.logistics.core.service.ProductCargoTypeRelService;
import com.sz.biz.logistics.core.service.ProductRegionRelService;
import com.sz.biz.logistics.core.service.ProductService;
import com.sz.biz.logistics.core.service.ProductServiceRelService;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: ProductController <br>
 * Author: zhengpenghui <br>
 * Date: 2017-04-26 16:30:00
 */
@RestController
@RequestMapping("${api}/cust/product")
@Api(description = " ", tags = "035、产品管理")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductServiceRelService productServiceRelService;

    @Autowired
    private ProductGroupService productGroupService;

    @Autowired
    private ProductCargoTypeRelService productCargoTypeRelService;

    @Autowired
    private ProductRegionRelService productRegionRelService;
    @Autowired
    private AirportService airportService;
    @Autowired
    private PortService portService;

    /**
     * 产品组信息、产品可选服务信息
     */
    @ApiOperation(value = "产品组信息、产品可选服务信息", notes = "产品组信息、产品可选服务信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品的id ", required = true, dataType = "Integer", paramType = "path")})
    @RequestMapping(value = "/serviceInfos/{id}", method = {RequestMethod.GET})
    public ResultDto getProductServiceInfos(@PathVariable Integer id) {
        ProductGroupOptServiceInfo productGroupOptServiceInfo = productService.getProductServiceInfos(id);
        ResultDto dto = new ResultDto();
        dto.setData(productGroupOptServiceInfo);
        return dto;
    }

    /**
     * 根据客户id校验客户的地址列表当前产品是否支持
     */
    @ApiOperation(value = "根据客户id校验客户的地址列表当前产品是否支持", notes = "-")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "产品uid", dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "customerId", value = "客户id", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "transportType", value = "地址类型", dataType = "int", paramType = "path")
    })
    @RequestMapping(value = "/{uid}/checkcustomer/{customerId}/{transportType}", method = {RequestMethod.GET})
    public ResultDto checkRegionByCustomerId(@PathVariable String uid, @PathVariable int customerId, @PathVariable int transportType) {
        return productService.checkRegionByCustomerId(uid, customerId, transportType);
    }

    /**
     * 根据产品id查询产品组信息
     */
    @ApiOperation(value = "根据产品id查询产品组信息", notes = "根据产品id查询产品组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品的id ", required = true, dataType = "Integer", paramType = "path")})
    @RequestMapping(value = "/group/{id}", method = {RequestMethod.GET})
    public ResultDto getProductGroupById(@PathVariable Integer id) {
        ProductGroupInfo productGroup = productService.getProductGroupById(id);
        ResultDto dto = new ResultDto();
        dto.setData(productGroup);
        return dto;
    }

    /**
     * 分页查询产品
     */
    //TODO 影响到的api
    @ApiOperation(value = "搜索产品", notes = "搜索产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "topGrade", value = "产品组： ", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "secondGrade", value = "二级产品组： ", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultDto findProductList(@RequestParam(required = false) String q,
                                     @RequestParam(required = false, defaultValue = "-1") int topGrade,
                                     @RequestParam(required = false, defaultValue = "-1") int secondGrade,
                                     @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                     @RequestParam(required = false, defaultValue = "10") int pageSize,
                                     @RequestParam(required = false) String sort,
                                     @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        QueryParams params = new QueryParams(pageSize, pageIndex, q, sort, asc);
        params.put("topGrade", topGrade);
        params.put("secondGrade", secondGrade);
        return productService.findByFilterCustomer(params);
    }

    /**
     * 产品列表初始化，返回产品id, uid,名称
     */
    @ApiOperation(value = "产品列表初始化，返回产品id, uid,名称", notes = "产品列表初始化，返回产品id, uid,名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = false, dataType = "int", paramType = "query")})
    @RequestMapping(value = "/list/short", method = {RequestMethod.GET})
    public ResultDto findProductListWithUidName(@RequestParam(required = false) String q,
                                                @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                                @RequestParam(required = false, defaultValue = "10") int pageSize) {
        q = StringUtils.replaceSqlPattern(q);
        QueryParams params = new QueryParams(pageSize, pageIndex, q, null);
        params.put("q", q);
        return productService.findProductListReturnUidName(params);
    }

    @ApiOperation(value = "所有产品组", notes = "所有产品组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "parentId（0：顶级）", dataType = "int", paramType = "query")})
    @RequestMapping(value = "/groups", method = {RequestMethod.GET})
    public ResultDto allProductGroups(@RequestParam(required = false, defaultValue = "0") int parentId) {
        List<ProductGroupDto> productGroupDtos = productGroupService.findByParentIdCustomer(parentId);
        ResultDto dto = new ResultDto();
        dto.setData(productGroupDtos);
        return dto;
    }

    /**
     * 根据产品id查询产品范围
     */
    @ApiOperation(value = "查询产品范围", notes = "查询产品范围")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品的id ", required = true, dataType = "Integer", paramType = "path")})
    @RequestMapping(value = "/region/{id}", method = {RequestMethod.GET})
    public ResultDto getRegionByProductId(@PathVariable Integer id) {
        List<ProductRegionCustomerDto> regionList = productRegionRelService.findRegionInfoByProductId(id);
        ResultDto dto = new ResultDto();
        dto.setData(regionList);
        return dto;
    }

    /**
     * 根据产品id查询产品
     */
    //TODO 影响到的API
    @ApiOperation(value = "查询产品", notes = "查询产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品的id ", required = true, dataType = "Integer", paramType = "path")})
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getProductById(@PathVariable Integer id) {
        ProductCustomerDto productDto = productService.findByIdCustomer(id);
        ResultDto dto = new ResultDto();
        dto.setData(productDto);
        return dto;
    }

    /**
     * 根据产品id查询产品范围
     */
    @ApiOperation(value = "查询产品货物类型", notes = "查询产品货物类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品的id ", required = true, dataType = "Integer", paramType = "path")})
    @RequestMapping(value = "/cargo/{id}", method = {RequestMethod.GET})
    public ResultDto getCargoByProductId(@PathVariable Integer id) {
        List<ProductCargotypeRelDto> cargoTypeList = productCargoTypeRelService.findByProductId(id);
        ResultDto dto = new ResultDto();
        dto.setData(cargoTypeList);
        return dto;
    }

    /**
     * 产品下可选服务
     */
    //TODO 影响到的API
    @ApiOperation(value = "产品下可选服务", notes = "产品下可选服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品的id ", required = true, dataType = "Integer", paramType = "path")})
    @RequestMapping(value = "/serviceoptional/{id}", method = {RequestMethod.GET})
    public ResultDto getOptionalServiceByProductId(@PathVariable Integer id) {
        List<ProductServiceTypeDto> optionalServices = productServiceRelService.findByProductIdAndServiceProperty(id, ProductServicePropertyEnum.OPTIONAL);
        ResultDto dto = new ResultDto();
        dto.setData(optionalServices);
        return dto;
    }


    /**
     * 根据重量以及产品UID校验当前产品是否支持
     */
    @ApiOperation(value = "根据重量以及产品UID校验当前产品是否支持", notes = "-")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "产品uid", dataType = "string", paramType = "path")
    })
    @RequestMapping(value = "/{uid}/checkweight", method = {RequestMethod.POST})
    public ResultDto checkWeight(@PathVariable String uid, @RequestBody @ApiParam(value = "重量对象", required = true) WeightDto weightDto) {
        return productService.checkWeight(uid, weightDto);
    }

    /**
     * 查询产品的揽收服务
     *
     * @param uid uid
     * @return ResultDto
     */
    @ApiOperation(value = "查询产品的揽收服务", notes = "查询产品的揽收服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "产品uid", required = true, dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/receiveservices", method = RequestMethod.GET)
    public ResultDto getProductReceiveInfo(@RequestParam String uid) {
        ProductReceiveInfoDto productReceiveInfoDto = productService.findProductReceiveInfoByUid(uid);
        ResultDto dto = new ResultDto();
        dto.setData(productReceiveInfoDto);
        return dto;
    }
    
    /**
     * 根据产品id和type查询国家在产品的范围
     */
    @ApiOperation(value = "查询产品范围国家信息", notes = "查询产品范围国家信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品的id ", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "from:起点(s) to:目的地(e) 揽收地址(l)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000",  dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/country", method = {RequestMethod.GET})
    public ResultDto getCountryByProductIdAndType(@RequestParam(required = true) Integer id,
    		@RequestParam(required = true) String type,
    		@RequestParam(required = false) String q,
    		@RequestParam(required = false, defaultValue = "1") int pageIndex,
            @RequestParam(required = false, defaultValue = "10") int pageSize) {
    	q = StringUtils.replaceSqlPattern(q);
        ResultDto dto = productService.getCountryByProductIdAndType(id, type,q,pageIndex,pageSize);
        return dto;
    }

    /**
     * 根据产品id,type和countryCode查询机场或港口
     */
    @ApiOperation(value = "查询产品范围内机场或港口", notes = "查询产品范围内机场或港口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品的id ", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "countryCode", value = "国家编码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "from:起点(s) to:目的地(e)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000",  dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/transport", method = {RequestMethod.GET})
    public ResultDto getTransportByProductIdAndType(@RequestParam(required = true) Integer id,
    		@RequestParam(required = true) String countryCode,
    		@RequestParam(required = false) String cityId,
    		@RequestParam(required = true) String type,
    		@RequestParam(required = false) String q,
    		@RequestParam(required = false, defaultValue = "1") int pageIndex,
            @RequestParam(required = false, defaultValue = "10") int pageSize) {
    	q = StringUtils.replaceSqlPattern(q);
        ResultDto dto = productService.getTransportByProductIdAndType(id, countryCode,cityId, type, q,pageIndex,pageSize);
        return dto;
    }

    /**
     * 根据产品id,type,countryCode和parentId查询地区
     */
    @ApiOperation(value = "查询地区", notes = "查询地区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品的id ", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "countryCode", value = "国家编码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父节点id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "from:起点(s) to:目的地(e) 揽收地址(l)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000",  dataType = "int", paramType = "query")
            })
    @RequestMapping(value = "/area", method = {RequestMethod.GET})
    public ResultDto getAreaByProductIdAndType(@RequestParam(required = true) Integer id,
    		@RequestParam(required = true) String countryCode,
    		@RequestParam(required = false) String parentId,
    		@RequestParam(required = true) String type,
    		@RequestParam(required = false) String q,
    		@RequestParam(required = false, defaultValue = "1") int pageIndex,
            @RequestParam(required = false, defaultValue = "10") int pageSize) {
    	q = StringUtils.replaceSqlPattern(q);
        ResultDto dto = productService.getAreaByProductIdAndType(id, countryCode,parentId,type,q,pageIndex,pageSize);
        return dto;
    }
    
    /**
     * 分页查询机场或港口
     *
     * @throws Exception
     */
    @ApiOperation(value = "查询机场或港口", notes = "查询机场或港口")
    @ApiImplicitParams({
    	    @ApiImplicitParam(name = "countryId", value = "国家必填项，这样才能区分到后台去查地区那张表", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "cityId", value = "城市", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型：1为港口，6为机场", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = true, dataType = "int", paramType = "query")
           })
    @RequestMapping(value = "/transportlist", method = {RequestMethod.GET})
    public
    @ResponseBody
    ResultDto findTransportList(@RequestParam(required = false) String countryId,
            @RequestParam(required = false) String cityId,
            @RequestParam(required = false) String q,
            @RequestParam(required = true) String type,
    		@RequestParam(required = false, defaultValue = "1") int pageIndex, 
    		@RequestParam(required = false, defaultValue = "10") int pageSize) {
    	ResultDto dto =null;
    	q = StringUtils.replaceSqlPattern(q);
        Map<String, Object> map = new HashMap<>();
        map.put("countryId", countryId);
        map.put("cityId", cityId);
        if(BaseConstants.TransportType.AIR.equals(type)){
        	dto = airportService.airportDropListPage(new QueryParams(pageSize, pageIndex, q, null, false).put(map));
        }
        else if(BaseConstants.TransportType.PORT.equals(type)){
        	dto = portService.portDropListPage(new QueryParams(pageSize, pageIndex, q, null, false).put(map));
        }
        return dto;
    }
}
